﻿@{
    List<Nop.Core.Domain.Directory.ExchangeRate> rates = ViewBag.Rates;
    bool autoUpdateEnabled = ViewBag.AutoUpdateEnabled ?? false;
    //page title
    ViewBag.Title = T("Admin.Configuration.Currencies").Text;
}
<div class="section-header">
    <div class="title">
        <img src="@Url.Content("~/Administration/Content/images/ico-configuration.png")" alt="" />
        @T("Admin.Configuration.Currencies")
    </div>
    <div class="options">
        <a href="javascript:document.CurrencySettings.submit()" class="k-button">@T("Admin.Common.Save")</a>
        <a href="@Url.Action("List", "Currency", new { liveRates = true })" class="k-button">@T("Admin.Configuration.Currencies.GetLiveRates")</a>
        <a href="@Url.Action("Create")" class="k-button">@T("Admin.Common.AddNew")</a>
    </div>
</div>
@using (Html.BeginForm("Save", "Currency", FormMethod.Post, new { name = "CurrencySettings" }))
{  
    <table class="adminContent">
        <tr>
            <td class="adminTitle">
                @T("Admin.Configuration.Currencies.Fields.ExchangeRateProvider"):
            </td>
            <td class="adminData">
                @(Html.DropDownList("exchangeRateProvider", ViewBag.ExchangeRateProviders as IEnumerable<SelectListItem>))
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @T("Admin.Configuration.Currencies.Fields.CurrencyRateAutoUpdateEnabled"):
            </td>
            <td class="adminData">
                @(Html.CheckBox("autoUpdateEnabled", autoUpdateEnabled)
                )
            </td>
        </tr>
    </table>
}
<table class="adminContent">
    <tr>
        <td>
            <div id="currencies-grid"></div>

            <script>
                $(document).ready(function () {
                    $("#currencies-grid").kendoGrid({
                        dataSource: {
                            type: "json",
                            transport: {
                                read: {
                                    url: "@Html.Raw(Url.Action("List", "Currency"))",
                                    type: "POST",
                                    dataType: "json"
                                }
                            },
                            schema: {
                                data: "Data",
                                total: "Total",
                                errors: "Errors"
                            },
                            error: function (e) {
                                display_kendoui_grid_error(e);
                                // Cancel the changes
                                this.cancelChanges();
                            },
                            serverPaging: true,
                            serverFiltering: true,
                            serverSorting: true
                        },
                        pageable: {
                            refresh: true,
                            numeric: false,
                            previousNext: false,
                            info: false
                        },
                        editable: {
                            confirmation: false,
                            mode: "inline"
                        },
                        scrollable: false,
                        columns: [{
                            field: "Name",
                            title: "@T("Admin.Configuration.Currencies.Fields.Name")"
                        }, {
                            field: "CurrencyCode",
                            title: "@T("Admin.Configuration.Currencies.Fields.CurrencyCode")",
                            width: 200
                        }, {
                            field: "Rate",
                            title: "@T("Admin.Configuration.Currencies.Fields.Rate")",
                            width: 200
                        }, {
                            field: "IsPrimaryExchangeRateCurrency",
                            title: "@T("Admin.Configuration.Currencies.Fields.IsPrimaryExchangeRateCurrency")",
                            width: 100,
                            headerAttributes: { style: "text-align:center" },
                            attributes: { style: "text-align:center" },
                            template: '<img src="@Url.Content("~/Administration/Content/images/")active-#=IsPrimaryExchangeRateCurrency#.gif" />'
                        }, {
                            field: "Id",
                            title: "@T("Admin.Configuration.Currencies.Fields.MarkAsPrimaryExchangeRateCurrency")",
                            template: '<a href="@Url.Content("~/Admin/Currency/MarkAsPrimaryExchangeRateCurrency/")#=Id#" class="k-button">@T("Admin.Configuration.Currencies.Fields.MarkAsPrimaryExchangeRateCurrency")</a>'
                        }, {
                            field: "IsPrimaryStoreCurrency",
                            title: "@T("Admin.Configuration.Currencies.Fields.IsPrimaryStoreCurrency")",
                            width: 100,
                            headerAttributes: { style: "text-align:center" },
                            attributes: { style: "text-align:center" },
                            template: '<img src="@Url.Content("~/Administration/Content/images/")active-#=IsPrimaryStoreCurrency#.gif" />'
                        }, {
                            field: "Id",
                            title: "@T("Admin.Configuration.Currencies.Fields.MarkAsPrimaryStoreCurrency")",
                            template: '<a href="@Url.Content("~/Admin/Currency/MarkAsPrimaryStoreCurrency/")#=Id#" class="k-button">@T("Admin.Configuration.Currencies.Fields.MarkAsPrimaryStoreCurrency")</a>'
                        }, {
                            field: "Published",
                            title: "@T("Admin.Configuration.Currencies.Fields.Published")",
                            width: 100,
                            headerAttributes: { style: "text-align:center" },
                            attributes: { style: "text-align:center" },
                            template: '<img src="@Url.Content("~/Administration/Content/images/")active-#=Published#.gif" />'
                        }, {
                            field: "DisplayOrder",
                            title: "@T("Admin.Catalog.Attributes.CheckoutAttributes.Values.Fields.DisplayOrder")",
                            width: 100
                        }, {
                            field: "Id",
                            title: "@T("Admin.Common.Edit")",
                            width: 100,
                            template: '<a href="Edit/#=Id#">@T("Admin.Common.Edit")</a>'
                        }]
                    });
                });
            </script>

        </td>
    </tr>
</table>
@if (rates != null)
{
    <h4>@T("Admin.Configuration.Currencies.LiveRates")</h4>
    <table class="adminContent" style="width: 300px">
        <tr>
            <td>

                <div id="rate-grid"></div>

                <script>

                    //local datasource
                    var rates = [
                        @for (int i = 0; i < rates.Count; i++)
                        {
                            var rate = rates[i];
                            <text>
{
    CurrencyCode: "@(Html.Raw(HttpUtility.JavaScriptStringEncode(rate.CurrencyCode)))",
    Rate: "@(Html.Raw(HttpUtility.JavaScriptStringEncode(rate.Rate.ToString())))"
}
</text>
                            if (i != rates.Count - 1)
                            {
                                <text>,</text>
                            }
                        }
                    ];

                    $(document).ready(function () {
                        $("#rate-grid").kendoGrid({
                            dataSource: {
                                data: rates,
                                schema: {
                                    model: {
                                        fields: {
                                            CurrencyCode: { type: "string" },
                                            Rate: { type: "number" }
                                        }
                                    }
                                },
                                error: function (e) {
                                    display_kendoui_grid_error(e);
                                    // Cancel the changes
                                    this.cancelChanges();
                                }
                            },
                            pageable: {
                                refresh: true,
                                numeric: false,
                                previousNext: false,
                                info: false
                            },
                            scrollable: false,
                            columns: [
                                {
                                    field: "CurrencyCode",
                                    title: "@T("Admin.Configuration.Currencies.Fields.CurrencyCode")",
                                    width: 100
                                }, {
                                    field: "Rate",
                                    title: "@T("Admin.Configuration.Currencies.Fields.Rate")",
                                    width: 100
                                },{
                                    field: "CurrencyCode",
                                    title: "@T("Admin.Configuration.Currencies.ApplyRate")",
                                    template: '<a href="@Url.Action("ApplyRate")?rate=#=Rate#&currencyCode=#=CurrencyCode#">@T("Admin.Configuration.Currencies.ApplyRate")</a>'
                                }
                            ]
                        });
                    });

                </script>
            </td>
        </tr>
    </table>
}
